package linuxbox.calendar.logic;

import java.sql.*;
import java.util.*;
import java.text.SimpleDateFormat;

import linuxbox.BoxStatic;

public class BoxCalendarEntry
{
  protected int m_iID;
  protected Timestamp m_FromTime;
  protected Timestamp m_ToTime;
  protected int m_UserID = -1;
  protected int m_CreatedBy = -1;
  protected String m_sSubject = "";
  protected String m_sDescription = "";
  protected String m_sCategory = "";
  protected int m_iSharing = 2;
  protected boolean m_bRepeat = false;
  protected boolean m_bIsAllDay = false;

  public void copyFrom(BoxCalendarEntry entry)
  {
    m_iID = entry.getID();
    m_iSharing = entry.getSharing();
    m_bRepeat = entry.getRepeat();
    m_bIsAllDay = entry.getIsAllDay();
    m_UserID = entry.getUserID();
    m_CreatedBy = entry.getCreatedBy();

    if (entry.getCategory() == null)
    {
      m_sCategory = null;
    }
    else
    {
      m_sCategory = new String(entry.getCategory());
    }
    if (entry.getSubject() == null)
    {
      m_sSubject = null;
    }
    else
    {
      m_sSubject = new String(entry.getSubject());
    }
    if (entry.getDescription() == null)
    {
      m_sDescription = null;
    }
    else
    {
      m_sDescription = new String(entry.getDescription());
    }
    if (entry.getFromTime() == null)
    {
      m_FromTime = null;
    }
    else
    {
      m_FromTime = new Timestamp(entry.getFromTime().getTime());
    }
    if (entry.getToTime() == null)
    {
      m_ToTime = null;
    }
    else
    {
      m_ToTime = new Timestamp(entry.getToTime().getTime());
    }
  }

  public int getID()
  {
    return m_iID;
  }

  public void setID(int id)
  {
    m_iID = id;
  }

  public int getUserID()
  {
    return m_UserID;
  }

  public void setUserID(int id)
  {
    m_UserID = id;
  }

  public int getCreatedBy()
  {
    return m_CreatedBy;
  }

  public void setCreatedBy(int s)
  {
    m_CreatedBy = s;
  }

  public Timestamp getFromTime()
  {
    return m_FromTime;
  }

  public void setFromTime(Timestamp tm)
  {
    m_FromTime = tm;
  }

  public Timestamp getToTime()
  {
    return m_ToTime;
  }

  public void setToTime(Timestamp tm)
  {
    m_ToTime = tm;
  }

  public String getSubject()
  {
    return m_sSubject;
  }

  public void setSubject(String s)
  {
    m_sSubject = s;
  }

  public String getDescription()
  {
    return m_sDescription;
  }

  public void setDescription(String s)
  {
    m_sDescription = s;
  }

  public String getCategory()
  {
    return m_sCategory;
  }

  public void setCategory(String s)
  {
    m_sCategory = s;
  }

  public int getSharing()
  {
    return m_iSharing;
  }

  public void setSharing(int i)
  {
    m_iSharing = i;
  }

  public boolean getRepeat()
  {
    return m_bRepeat;
  }

  public void setRepeat(boolean b)
  {
    m_bRepeat = b;
  }

  public boolean getIsAllDay()
  {
    return m_bIsAllDay;
  }

  public void setIsAllDay(boolean b)
  {
    m_bIsAllDay = b;
  }

  public int getFromHour(TimeZone tz)
  {
    Calendar tmpTime = Calendar.getInstance(tz);
    tmpTime.setTime(m_FromTime);
    return tmpTime.get(Calendar.HOUR_OF_DAY);
  }

  public int getToHour(TimeZone tz)
  {
    Calendar tmpTime = Calendar.getInstance(tz);

    if (m_ToTime != null)
    {
      tmpTime.setTime(m_ToTime);
      int toHour = tmpTime.get(Calendar.HOUR_OF_DAY);

      if ( ( (tmpTime.getTime().getTime()) % (60 * 60 * 1000)) >= 60 * 1000) //greater than 1 min
      {
        toHour++;
      }
      return toHour;
    }
    else
    {
      tmpTime.setTime(m_FromTime);
    }
    return tmpTime.get(Calendar.HOUR_OF_DAY);
  }

  public String getTimeRangeString(TimeZone tz)
  {
    if (m_FromTime == null)
    {
      return "";
    }
    SimpleDateFormat dateTimeFormatter = new SimpleDateFormat(BoxStatic.SHORT_TIME_FORMAT, Locale.US);
    dateTimeFormatter.setCalendar(Calendar.getInstance(tz));

    String s = dateTimeFormatter.format(m_FromTime) + "-";
    if (m_ToTime != null)
    {
      s += dateTimeFormatter.format(m_ToTime);
    }
    return s;
  }
/*
  public String getDateString()
  {
    BoxStatic.dateTimeFormatter.applyPattern(BoxStatic.SHORT_DATE_FORMAT);
    return BoxStatic.dateTimeFormatter.format(m_FromTime);
  }

  public String getFromTimeString()
  {
    BoxStatic.dateTimeFormatter.applyPattern(BoxStatic.SHORT_TIME_FORMAT);
    return BoxStatic.dateTimeFormatter.format(m_FromTime);
  }

  public String getToTimeString()
  {
    BoxStatic.dateTimeFormatter.applyPattern(BoxStatic.SHORT_TIME_FORMAT);
    return BoxStatic.dateTimeFormatter.format(m_FromTime);
  }
*/
  public String toString()
  {
    String s = "id=" + m_iID
        + ", m_FromTime=" + m_FromTime
        + ", m_ToTime=" + m_ToTime
        + ", m_UserID=" + m_UserID
        + ", m_CreatedBy=" + m_CreatedBy
        + ", m_sSubject=" + m_sSubject
        + ", m_sDescription=" + m_sDescription
        + ", m_sCategory=" + m_sCategory
        + ", m_iSharing=" + m_iSharing
        + ", m_bRepeat=" + m_bRepeat
        + ", m_bIsAllDay=" + m_bIsAllDay
        + "\n";
    return s;
  }
}
